package com.ykdl.member.kid.util;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.TencentWbShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ActivityDetailBean;

public class UmShareUtils {
	private final UMSocialService mController = UMServiceFactory
			.getUMSocialService(Constants.DESCRIPTOR);
	private SHARE_MEDIA mPlatform = SHARE_MEDIA.SINA;
	private Activity activity;
	private ActivityDetailBean mBean;

	public UmShareUtils(Activity context) {
		activity = context;
		configPlatforms();
	}

	/**
	 * 配置分享平台参数</br>
	 */
	private void configPlatforms() {
		// 添加新浪SSO授权
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
		mController.getConfig().setSinaCallbackUrl("");
		// 添加腾讯微博SSO授权
		mController.getConfig().setSsoHandler(new TencentWBSsoHandler());

		// 添加QQ、QZone平台
		// addQQQZonePlatform();

		// 添加微信、微信朋友圈平台
		addWXPlatform();

	}

	/**
	 * 直接分享，底层分享接口。如果分享的平台是新浪、腾讯微博、豆瓣、人人，则直接分享，无任何界面弹出； 其它平台分别启动客户端分享</br>
	 */
	public void directShare() {
		mController.directShare(activity, mPlatform, new SnsPostListener() {

			@Override
			public void onStart() {
			}

			@Override
			public void onComplete(SHARE_MEDIA platform, int eCode,
					SocializeEntity entity) {
				String showText = "分享成功";
				if (eCode != StatusCode.ST_CODE_SUCCESSED) {
					showText = "分享失败 [" + eCode + "]";
				}
				Toast.makeText(activity, showText, Toast.LENGTH_SHORT).show();
			}
		});
	}

	/**
	 * @功能描述 : 添加QQ平台支持 QQ分享的内容， 包含四种类型， 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary,
	 *       image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title :
	 *       要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl
	 *       : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 )
	 * @return
	 */
	private void addQQQZonePlatform() {/*
										 * String appId = "100522490"; String
										 * appKey =
										 * "35031d771ded98c061ae41b9e0b6820f";
										 * // 添加QQ支持, 并且设置QQ分享内容的target url
										 * UMQQSsoHandler qqSsoHandler = new
										 * UMQQSsoHandler(activity, appId,
										 * appKey); qqSsoHandler.setTargetUrl(
										 * "http://www.ykdllmyr.com/files/lmyr_latest.apk"
										 * ); qqSsoHandler.addToSocialSDK();
										 * 
										 * // 添加QZone平台 QZoneSsoHandler
										 * qZoneSsoHandler = new
										 * QZoneSsoHandler(activity, appId,
										 * appKey);
										 * qZoneSsoHandler.addToSocialSDK();
										 */
	}

	/**
	 * @功能描述 : 添加微信平台分享
	 * @return
	 */
	private void addWXPlatform() {
		// 注意：在微信授权的时候，必须传递appSecret
		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		String appId = "wx1594ffac39018603";
		String appSecret = "f10dd0df7e44e2af5b74fb531d3e6ff5";
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(activity, appId, appSecret);
		wxHandler.addToSocialSDK();

		// 支持微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(activity, appId,
				appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
	}

	/**
	 * 调用显示分享界面
	 */
	public void OpenShare(String coment, String imageUrl) {

		String sharecontent = "我在辣妈惠生活，快来围观吧！" + coment
				+ "，【苹果、安卓手机免费下载】http://www.ykdllmyr.com/files/hsh_latest.apk";
		mController.setShareContent(sharecontent);
		setShareContent(sharecontent, imageUrl);
		mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN,
				SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA,
				SHARE_MEDIA.TENCENT);
		mController.openShare(activity, false);// 使用友盟的默认分享面板
	}

	/**
	 * 自定义分享（分享到微信）
	 * 
	 * @param coment
	 * @param imageUrl
	 */
	public void MyWeiXinShare(ActivityDetailBean bean) {
		mBean=bean;
		String sharecontent =mBean.getNote();// "我在辣妈惠生活，快来围观吧！" + coment+ "，【苹果、安卓手机免费下载】http://www.ykdllmyr.com/files/hsh_latest.apk";
		mController.setShareContent(sharecontent);
		setShareContent(sharecontent, bean.getShare_pic().getUrl());
//		mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN);

		mController.postShare(activity, SHARE_MEDIA.WEIXIN,
				new SnsPostListener() {
					@Override
					public void onStart() {
//						Toast.makeText(activity, "开始分享.", Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onComplete(SHARE_MEDIA platform, int eCode,
							SocializeEntity entity) {
						if (eCode == 200) {
//							Toast.makeText(activity, "分享成功.",Toast.LENGTH_SHORT).show();
						} else {
							String eMsg = "";
							if (eCode == -101) {
								eMsg = "没有授权";
							}
							Toast.makeText(activity,
									"分享失败[" + eCode + "] " + eMsg,
									Toast.LENGTH_SHORT).show();
						}
					}
				});
	}

	/**
	 * 自定义分享（分享到微信朋友圈）
	 * 
	 * @param coment
	 * @param imageUrl
	 */
	public void MyCircleShare(ActivityDetailBean bean) {
		mBean=bean;
		String sharecontent = mBean.getNote();// "我在辣妈惠生活，快来围观吧！" + coment+ "，【苹果、安卓手机免费下载】http://www.ykdllmyr.com/files/hsh_latest.apk";
		mController.setShareContent(sharecontent);
		setShareContent(sharecontent, bean.getShare_pic().getUrl());
//		mController.getConfig().setPlatforms(SHARE_MEDIA.WEIXIN_CIRCLE);

		mController.postShare(activity, SHARE_MEDIA.WEIXIN_CIRCLE,
				new SnsPostListener() {
					@Override
					public void onStart() {
//						Toast.makeText(activity, "开始分享.", Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onComplete(SHARE_MEDIA platform, int eCode,
							SocializeEntity entity) {
						if (eCode == 200) {
//							Toast.makeText(activity, "分享成功.",Toast.LENGTH_SHORT).show();
						} else {
							String eMsg = "";
							if (eCode == -101) {
								eMsg = "没有授权";
							}
							Toast.makeText(activity,
									"分享失败[" + eCode + "] " + eMsg,
									Toast.LENGTH_SHORT).show();
						}
					}
				});
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(
				requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	/**
	 * 根据不同的平台设置不同的分享内容
	 */
	private void setShareContent(String coment, String imageUrl) {

		UMImage localImage = new UMImage(activity, R.drawable.share_image);
		UMImage urlImage = null;
		if (!TextUtils.isEmpty(imageUrl)) {
			urlImage = new UMImage(activity, imageUrl);
		}

		// 设置微信内容
		WeiXinShareContent weixinContent = new WeiXinShareContent();
		weixinContent.setShareContent(coment);
		weixinContent.setTitle(mBean.getTitle());
		weixinContent.setTargetUrl(mBean.getInvitation_url());
		if (urlImage != null) {
			weixinContent.setShareImage(urlImage);
		} else {
			weixinContent.setShareImage(localImage);
		}
		mController.setShareMedia(weixinContent);

		// 设置朋友圈分享的内容
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setShareContent(coment);
		circleMedia.setTitle(mBean.getTitle());
		if (urlImage != null) {
			circleMedia.setShareImage(urlImage);
		} else {
			circleMedia.setShareImage(localImage);
		}
		// circleMedia.setShareMedia(urlImage);

		circleMedia
				.setTargetUrl(mBean.getInvitation_url());//http://www.ykdllmyr.com/files/lmyr_latest.apk
		mController.setShareMedia(circleMedia);

		// 设置QQ空间分享内容
		QZoneShareContent qzone = new QZoneShareContent();
		qzone.setShareContent(coment);
		qzone.setTargetUrl("http://www.ykdllmyr.com/files/lmyr_latest.apk");
		qzone.setTitle("辣妈育儿");
		if (urlImage != null) {
			qzone.setShareImage(urlImage);
		} else {
			qzone.setShareImage(localImage);
		}

		// qzone.setShareMedia(uMusic);
		mController.setShareMedia(qzone);
		// 设置QQ分享内容
		QQShareContent qqShareContent = new QQShareContent();
		qqShareContent.setShareContent(coment);
		qqShareContent.setTitle("辣妈育儿");
		if (urlImage != null) {
			qqShareContent.setShareImage(urlImage);
		} else {
			qqShareContent.setShareImage(localImage);
		}
		qqShareContent
				.setTargetUrl("http://www.ykdllmyr.com/files/lmyr_latest.apk");
		mController.setShareMedia(qqShareContent);

		// 设置QQ微博内容
		TencentWbShareContent tencent = new TencentWbShareContent();
		tencent.setShareContent(coment);
		tencent.setTargetUrl("http://www.ykdllmyr.com/files/lmyr_latest.apk");
		tencent.setTitle("辣妈育儿");
		if (urlImage != null) {
			tencent.setShareImage(urlImage);
		} else {
			tencent.setShareImage(localImage);
		}
		// 设置tencent分享内容
		mController.setShareMedia(tencent);
		// 设置SINA微博内容
		SinaShareContent sinaContent = new SinaShareContent();
		sinaContent.setShareContent(coment);
		sinaContent
				.setTargetUrl("http://www.ykdllmyr.com/files/lmyr_latest.apk");
		sinaContent.setTitle("辣妈育儿");
		if (urlImage != null) {
			sinaContent.setShareImage(urlImage);
		} else {
			sinaContent.setShareImage(localImage);
		}
		mController.setShareMedia(sinaContent);

	}
}
